package com.example.employeemanagement.Controller;

import com.example.employeemanagement.entry.Department;
import com.example.employeemanagement.service.DepartmentService;
import com.example.employeemanagement.vo.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 部门管理控制器
 * 提供部门相关的REST API接口
 */
@RestController
@RequestMapping("/departments")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取所有部门
     * @return 部门列表及操作结果信息
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<Department>>> getAllDepartments() {
        try {
            List<Department> departments = departmentService.getAllDepartments();
            return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", departments));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 根据ID获取部门
     * @param departmentId 部门ID
     * @return 部门信息及操作结果信息
     */
    @GetMapping("/{departmentId}")
    public ResponseEntity<ApiResponse<Department>> getDepartmentById(@PathVariable Integer departmentId) {
        try {
            Optional<Department> department = departmentService.getDepartmentById(departmentId);
            if (department.isPresent()) {
                return ResponseEntity.ok(new ApiResponse<>("success", "查询成功", department.get()));
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(new ApiResponse<>("error", "部门不存在: ID " + departmentId, null));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 添加部门
     * @param department 部门信息
     * @return 操作结果信息
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Department>> addDepartment(@Valid @RequestBody Department department) {
        try {
            String result = departmentService.addDepartment(department);
            if (result.startsWith("部门添加成功")) {
                return ResponseEntity.status(HttpStatus.CREATED)
                        .body(new ApiResponse<>("success", result, department));
            } else {
                return ResponseEntity.badRequest().body(new ApiResponse<>("error", result, null));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 更新部门
     * @param departmentId 部门ID
     * @param department 更新的部门信息
     * @return 操作结果信息
     */
    @PutMapping("/{departmentId}")
    public ResponseEntity<ApiResponse<Department>> updateDepartment(
            @PathVariable Integer departmentId,
            @Valid @RequestBody Department department) {
        try {
            String result = departmentService.updateDepartment(departmentId, department);
            if (result.equals("部门更新成功")) {
                // 获取更新后的部门信息
                Optional<Department> updatedDepartment = departmentService.getDepartmentById(departmentId);
                if (updatedDepartment.isPresent()) {
                    return ResponseEntity.ok(new ApiResponse<>("success", result, updatedDepartment.get()));
                } else {
                    return ResponseEntity.ok(new ApiResponse<>("success", result, null));
                }
            } else {
                return ResponseEntity.badRequest().body(new ApiResponse<>("error", result, null));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }

    /**
     * 删除部门
     * @param departmentId 部门ID
     * @return 操作结果信息
     */
    @DeleteMapping("/{departmentId}")
    public ResponseEntity<ApiResponse<Void>> deleteDepartment(@PathVariable Integer departmentId) {
        try {
            String result = departmentService.deleteDepartment(departmentId);
            if (result.equals("部门删除成功")) {
                return ResponseEntity.ok(new ApiResponse<>("success", result, null));
            } else {
                return ResponseEntity.badRequest().body(new ApiResponse<>("error", result, null));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(new ApiResponse<>("error", e.getMessage(), null));
        }
    }
}